package com.kfm.web.controller.system;

import com.kfm.common.domain.response.Result;
import com.kfm.common.util.Constant;
import com.kfm.common.util.RSAUtils;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class KeyController {
    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/getKey")
    public Result getPublicKey() {
        String uuid = RSAUtils.uuid();
        Map<String, String> keys = RSAUtils.getKey();
        redisTemplate.opsForValue().set(uuid, keys.get(Constant.PRIVATE_KEY), 30, TimeUnit.MINUTES);
        return Result.success("查询公钥成功", Map.of("uuid", uuid, Constant.PUBLIC_KEY, keys.get(Constant.PRIVATE_KEY)));
    }


    @RequestMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        GifCaptcha gifCaptcha = new GifCaptcha(130, 45, 4);
        CaptchaUtil.out(gifCaptcha, request, response);
    }
}
